package com.example.hello.config;

import com.example.hello.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Configuration
public class FileStorageConfig {

    @Value("${storage.type:local}")
    private String storageType;

    @Autowired
    @Qualifier("fileStorageServiceImpl")
    private FileStorageService objectStorageService;
    
    @Autowired
    @Qualifier("localFileStorageService")
    private FileStorageService localFileStorageService;
    
    @Bean
    @Primary
    public FileStorageService fileStorageService() {
        // 根据配置选择存储服务
        if ("object".equalsIgnoreCase(storageType)) {
            return objectStorageService;
        } else {
            return localFileStorageService;
        }
    }
    
    /**
     * 带有故障转移功能的文件存储服务
     */
    public static class FallbackFileStorageService implements FileStorageService {
        
        private final FileStorageService primaryService;
        private final FileStorageService fallbackService;
        
        public FallbackFileStorageService(FileStorageService primaryService, FileStorageService fallbackService) {
            this.primaryService = primaryService;
            this.fallbackService = fallbackService;
        }
        
        @Override
        public String uploadFile(MultipartFile file, String directory) throws IOException {
            try {
                return primaryService.uploadFile(file, directory);
            } catch (Exception e) {
                // 主服务失败，使用备用服务
                return fallbackService.uploadFile(file, directory);
            }
        }
        
        @Override
        public Map<String, String> uploadFiles(List<MultipartFile> files, String directory) throws IOException {
            try {
                return primaryService.uploadFiles(files, directory);
            } catch (Exception e) {
                // 主服务失败，使用备用服务
                return fallbackService.uploadFiles(files, directory);
            }
        }
        
        @Override
        public boolean deleteFile(String fileUrl) {
            try {
                return primaryService.deleteFile(fileUrl);
            } catch (Exception e) {
                // 主服务失败，使用备用服务
                return fallbackService.deleteFile(fileUrl);
            }
        }
        
        @Override
        public String generatePresignedUrl(String objectKey, int expirationMinutes) {
            try {
                return primaryService.generatePresignedUrl(objectKey, expirationMinutes);
            } catch (Exception e) {
                // 主服务失败，使用备用服务
                return fallbackService.generatePresignedUrl(objectKey, expirationMinutes);
            }
        }
    }
} 